
import { _decorator, BoxCollider2D, Collider, Collider2D, Component, Contact2DType, Director, director, ITriggerEvent, log, Node, ParticleSystem2D, v2, v3 } from 'cc';
import { EnemyMgr } from '../mainScene/EnemyMgr';
import { getDistance, lookAtTarget } from '../utils/utils';
import { gameMgr } from '../mainScene/gameMgr';
import { playerSkillTypeEnum } from '../Enum';
import { Enemy } from '../mainScene/Enemy';
import { Player } from '../mainScene/Player';
import { PoolMgr } from '../PoolMgr';

const { ccclass, property } = _decorator;

@ccclass('FireBall')
export class FireBall extends Component {

    attackTargetPos=null;

    tempPos=v3();

    init() {
        this.poolReset();

        //获取目标敌人
        this.attackTargetPos = EnemyMgr.ins.getNearlyExistOneEnemyNodeNottTornado().position;

        let collider = this.node.getComponent(BoxCollider2D);
        collider.on(Contact2DType.BEGIN_CONTACT, this.onTriggerFuc, this);

        lookAtTarget(this.attackTargetPos,this.node,30);
        gameMgr.ins.targetLineMove(this.attackTargetPos,this.node,Player.ins.fireBallSpeed)

    }
    poolReset() {//重新设置，针对预制体池
        this.attackTargetPos = null;
        this.tempPos = v3();
    }

    // protected update(dt: number): void {
    //     if (getDistance(this.node.position.x,this.node.position.y,this.attackTargetPos.x,this.attackTargetPos.y)<15) {
    //         this.isOpenTrigg=true
    //     }

    //     if (!gameMgr.ins.isGamePause) {
    //         gameMgr.ins.trackTargetMove(this.attackTargetPos, this.node, Player.ins.fireBallSpeed, dt)
    //     }

    // }

    onTriggerFuc(self: Collider, other: Collider) {
            if (other.node.getComponent(BoxCollider2D).tag==1) {
                director.once(Director.EVENT_AFTER_PHYSICS,()=>{
                    other.node.getComponent(Enemy).hurt(playerSkillTypeEnum.fireBall);
                    if(this.node){
                        this.recycle();
                    }
                },this)
            }
    }

    recycle(){
        // this.node.destroy();
        PoolMgr.ins.poolPut(this.node.name,this.node);
            
    }
 
}


